package libase.web.jsf;

import java.math.BigDecimal;
import java.util.Map;
import javax.faces.context.FacesContext;

public class Param
{
    public static BigDecimal get(String pName) throws ParamCastException
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> map = context.getExternalContext().getRequestParameterMap();

        BigDecimal id;

        try
        {
            id = new BigDecimal(map.get(pName));
        }
        catch (NumberFormatException e)
        {
            throw new ParamCastException();
        }
        catch (NullPointerException e)
        /* If no such param in the map */
        {
            throw new ParamCastException();
        }

        return id;
    }

    public static String gets(String pName) throws ParamCastException
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> map = context.getExternalContext().getRequestParameterMap();

        String id;

        try
        {
            id = map.get(pName);
        }
        catch (NullPointerException e)
        /* If no such param in the map */
        {
            throw new ParamCastException();
        }

        if (id == null)
        {
            throw new ParamCastException();
        }

        return id;
    }
}
